package cn.iocoder.yudao.module.exam.controller.admin.questions;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.exam.controller.admin.questions.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.questions.QuestionsDO;
import cn.iocoder.yudao.module.exam.service.questions.QuestionsService;

@Tag(name = "管理后台 - 考试题库")
@RestController
@RequestMapping("/exam/questions")
@Validated
public class QuestionsController {

    @Resource
    private QuestionsService questionsService;

    @PostMapping("/create")
    @Operation(summary = "创建考试题库")
    @PreAuthorize("@ss.hasPermission('exam:questions:create')")
    public CommonResult<Long> createQuestions(@Valid @RequestBody QuestionsSaveReqVO createReqVO) {
        return success(questionsService.createQuestions(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考试题库")
    @PreAuthorize("@ss.hasPermission('exam:questions:update')")
    public CommonResult<Boolean> updateQuestions(@Valid @RequestBody QuestionsSaveReqVO updateReqVO) {
        questionsService.updateQuestions(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除考试题库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('exam:questions:delete')")
    public CommonResult<Boolean> deleteQuestions(@RequestParam("id") Long id) {
        questionsService.deleteQuestions(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除考试题库")
                @PreAuthorize("@ss.hasPermission('exam:questions:delete')")
    public CommonResult<Boolean> deleteQuestionsList(@RequestParam("ids") List<Long> ids) {
        questionsService.deleteQuestionsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考试题库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('exam:questions:query')")
    public CommonResult<QuestionsRespVO> getQuestions(@RequestParam("id") Long id) {
        QuestionsDO questions = questionsService.getQuestions(id);
        return success(BeanUtils.toBean(questions, QuestionsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考试题库分页")
    @PreAuthorize("@ss.hasPermission('exam:questions:query')")
    public CommonResult<PageResult<QuestionsRespVO>> getQuestionsPage(@Valid QuestionsPageReqVO pageReqVO) {
        PageResult<QuestionsDO> pageResult = questionsService.getQuestionsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QuestionsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考试题库 Excel")
    @PreAuthorize("@ss.hasPermission('exam:questions:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQuestionsExcel(@Valid QuestionsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QuestionsDO> list = questionsService.getQuestionsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考试题库.xls", "数据", QuestionsRespVO.class,
                        BeanUtils.toBean(list, QuestionsRespVO.class));
    }

}